package ro.ase.catalog.security.dao.hibernate;

import ro.ase.catalog.beans.Absence;
import ro.ase.catalog.beans.Student;
import ro.ase.catalog.beans.Teacher;
import ro.ase.catalog.beans.dto.AbsenceDTO;
import ro.ase.catalog.dao.hibernate.GenericDAOImpl;
import ro.ase.catalog.security.dao.IAbsenceDAO;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Ioana
 * Date: 6/14/14
 * Time: 5:45 PM
 * To change this template use File | Settings | File Templates.
 */
public class AbsenceDAOImpl extends GenericDAOImpl<Absence, Integer> implements IAbsenceDAO {
    public List<AbsenceDTO> displayAbsencesByStudent(Student student, Teacher teacher) {
        List<AbsenceDTO> absenceDTOs=new ArrayList<AbsenceDTO>();
        List<Absence> absenceList=getHibernateTemplate().find("select abs from Absence abs where abs.student = ? and abs.teacherDisciplineClass.teacher=?", new Object[]{student, teacher} );
        for(int i=0;i<absenceList.size();i++)
        {
            AbsenceDTO gradeDTO=new AbsenceDTO();
            gradeDTO.setId(absenceList.get(i).getId());
            if(true==(absenceList.get(i).getMotivata()))
                 gradeDTO.setMotivata("Motivata");
            else
                gradeDTO.setMotivata("Nemotivata");
            gradeDTO.setComment(absenceList.get(i).getComment());
            gradeDTO.setDate(absenceList.get(i).getDate().toString().substring(0,10));
            absenceDTOs.add(gradeDTO);
        }
        return absenceDTOs;
    }

    public List<AbsenceDTO> displayAbsencesByStudent(Student student, Teacher teacher, Date date) {
        List<AbsenceDTO> absenceDTOs=new ArrayList<AbsenceDTO>();
        List<Absence> absenceList=getHibernateTemplate().find("select abs from Absence abs where abs.student = ? and abs.teacherDisciplineClass.teacher=? and abs.date<=?", new Object[]{student, teacher,date} );
        for(int i=0;i<absenceList.size();i++)
        {
            AbsenceDTO gradeDTO=new AbsenceDTO();
            gradeDTO.setId(absenceList.get(i).getId());
            if(true==(absenceList.get(i).getMotivata()))
                gradeDTO.setMotivata("Motivata");
            else
                gradeDTO.setMotivata("Nemotivata");
            gradeDTO.setComment(absenceList.get(i).getComment());
            gradeDTO.setDate(absenceList.get(i).getDate().toString().substring(0,10));
            absenceDTOs.add(gradeDTO);
        }
        return absenceDTOs;
    }
}
